package com.whq.thrift.api.web.admin.service.baseconf.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.admin.db.urlmapping.UrlMappingInfo;
import com.whq.thrift.api.web.admin.db.urlmapping.UrlMappingInfoMapper;
import com.whq.thrift.api.web.admin.service.baseconf.UrlMappingInfoService;

@Service
public class UrlMappingInfoServiceImpl extends UrlMappingInfoService {
	@Autowired
	private UrlMappingInfoMapper urlMappingInfoMapper;

	public void addUrlMappingInfo(UrlMappingInfo urlMappingInfo) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");

		if (urlMappingInfo == null) {
			return;
		}
		Date now = new Date();
		urlMappingInfo.setCreateTime(now);
		urlMappingInfo.setUpdateTime(now);

		this.urlMappingInfoMapper.addUrlMappingInfo(urlMappingInfo);
	}

	public void deleteUrlMappingInfo(String url) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");

		this.urlMappingInfoMapper.deleteUrlMappingInfo(url);
	}

	public void updateUrlMappingInfo(UrlMappingInfo urlMappingInfo) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		if (urlMappingInfo == null) {
			return;
		}
		Date now = new Date();
		urlMappingInfo.setUpdateTime(now);

		this.urlMappingInfoMapper.updateUrlMappingInfo(urlMappingInfo);
	}

	public List<UrlMappingInfo> findUrlMappingInfoByMap(Map<String, Object> whereParams, int beginIndex, int maxCount) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");

		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		whereParams.put("beginIndex", beginIndex);
		whereParams.put("maxCount", maxCount);
		return this.urlMappingInfoMapper.findUrlMappingInfoByMap(whereParams);
	}

	public int findUrlMappingInfoCount(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");

		return this.urlMappingInfoMapper.findUrlMappingInfoCount(whereParams);
	}

	public UrlMappingInfo findUrlMappingInfo(String url) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");

		return this.urlMappingInfoMapper.findUrlMappingInfo(url);
	}

	@Override
	public List<UrlMappingInfo> findUrlMappingApiList(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		return this.urlMappingInfoMapper.findUrlMappingApiList(whereParams);
	}

}